package cn.cutie.clot.gateway.plugin;

import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Description:
 * @Author: Cutie
 * @CreateDate: 2024/6/5 01:40
 * @Version: 0.0.1
 */
public class DefaultGatewayPluginChain implements GatewayPluginChain{

    List<GatewayPlugin> plugins;

    int index = 0;

    public DefaultGatewayPluginChain(List<GatewayPlugin> plugins) {
        this.plugins = plugins;
    }

    @Override
    public Mono<Void> handle(ServerWebExchange exchange) {
        // defer 表示惰性的
        return Mono.defer(() -> {
            if (index < plugins.size()){
               return plugins.get(index++).handle(exchange, this);
            }
            return Mono.empty();
        });
    }
}
